package com.xzy.service.impl;

import com.xzy.dao.IBookDao;
import com.xzy.dao.impl.BookDaoImpl;
import com.xzy.domain.Book;
import com.xzy.domain.BookPage;
import com.xzy.service.IBookService;

import java.util.List;

public class BookServiceImpl implements IBookService {
    private IBookDao dao= new BookDaoImpl();
    @Override
    public int addBook(Book book) {
        return dao.addBook(book);
    }

    @Override
    public int DeleteByid(Integer id) {
        return dao.deleteByid(id);
    }

    @Override
    public int updateBook(Book book) {
        return dao.updataBook(book);
    }

    @Override
    public Book quaryByid(Integer id) {
        return dao.quaryByid(id);
    }

    @Override
    public List<Book> quaryBooks() {
        return dao.quaryBooks();
    }


    @Override
    public BookPage<Book> getPage(Integer pageOn, Integer pageSize) {
        BookPage<Book> Page = new BookPage<>();
        Page.setPageOn(pageOn);
        Page.setPageSize(pageSize);
        Integer totalCount = dao.quaryTotalCount();
        Integer TotalPage = totalCount / pageSize;
        if (totalCount % pageSize != 0) {
            TotalPage++;
        }

        Page.setTotalCount(totalCount);
        Page.setTotalPage(TotalPage);
        Integer bagin = (pageOn - 1) * pageSize;
        List<Book> items = dao.quaryBooksByPage(bagin, pageSize);
        Page.setItems(items);
        return Page;
    }

    @Override
    public BookPage<Book> getPageByPrice(Integer pageOn, Integer pageSize, Integer min, Integer max) {
        BookPage<Book> page = new BookPage<>();
        page.setPageOn(pageOn);
        page.setPageSize(pageSize);
        Integer totalCount = dao.quaryTotalCountByPrice(min,max);
        Integer TotalPage = totalCount / pageSize;
        if (totalCount % pageSize != 0) {
            TotalPage++;
        }

        page.setTotalCount(totalCount);
        page.setTotalPage(TotalPage);
        Integer bagin = (pageOn - 1) * pageSize;

        page.setItems(dao.quaryPageByPrice(bagin, pageSize,min,max));
        return  page;
    }
}
